﻿#include "slaveinfor.h"
#include "ui_slaveinfor.h"
#include <QDebug>

SlaveInfor::SlaveInfor(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SlaveInfor)
{
    ui->setupUi(this);

    this->MsgDlg = new class QMessageBox();

    ui->LineEdit_FileName->setEnabled(false);

    ui->LineEdit_SlaveNum->setEnabled(false);
}

SlaveInfor::~SlaveInfor()
{
    delete ui;
}

// 判断从机软件版本号是否合法，软件版本号只能是数字字符
bool SlaveInfor::CheckSWVerIsLegal(QString strSWVer)
{
    bool bLegal = true;
    int i=0;

    // 遍历strSWVer
    while(i < strSWVer.length())
    {
        if(strSWVer.at(i) > '9' || strSWVer.at(i) < '0')
        {
            bLegal = false;
            break;
        }
        i++;
    }

    return bLegal;
}

// 判断从机软件标识符是否合法，软件标识符只能是数字、字母、下划线组合
bool SlaveInfor::CheckSWLableIsLegal(QString strSWLable)
{
    bool bLegal = true;
    int i=0;

    // 遍历strSWLable
    while(i < strSWLable.length())
    {
        if((strSWLable.at(i) >= '0' && strSWLable.at(i) <= '9')
            || (strSWLable.at(i) >= 'A' && strSWLable.at(i) <= 'Z')
                || (strSWLable.at(i) >= 'a' && strSWLable.at(i) <= 'z')
                    || strSWLable.at(i) == '_')
        {
            i++;
            continue;
        }
        else
        {
            bLegal = false;
            break;
        }
    }

    return bLegal;
}

void SlaveInfor::SlaveInfor_SetSlaveName(QString strName)
{
    ui->LineEdit_FileName->setText(strName);
}

void SlaveInfor::SlaveInfor_SetSlaveLable(QString strLable)
{
    ui->LineEdit_SWLable->setText(strLable);
}

void SlaveInfor::SlaveInfor_SetSlaveVer(QString strVer)
{
    ui->LineEdit_SWVer->setText(strVer);
}

void SlaveInfor::SlaveInfor_ClearSlaveSWLable(void)
{
    ui->LineEdit_SWLable->clear();
}

void SlaveInfor::SlaveInfor_ClearSlaveSWVer(void)
{
    ui->LineEdit_SWVer->clear();
}

void SlaveInfor:: SlaveInfor_SetSlaveNum(QString strNum)
{
    ui->LineEdit_SlaveNum->setText(strNum);
}

void SlaveInfor::on_Btn_Reset_clicked()
{
    // 清空版本号和软件标识符
    ui->LineEdit_SWLable->clear();
    ui->LineEdit_SWVer->clear();
}

void SlaveInfor::on_Btn_Save_clicked()
{
    // 获取软件标识符
    QString strSWLable = ui->LineEdit_SWLable->text();
    // 获取版本号
    QString strSWVer = ui->LineEdit_SWVer->text();

    // 软件标识符格式非法，或是空字符串
    if(false == this->CheckSWLableIsLegal(strSWLable)
       || strSWLable.isEmpty())
    {
        // Critical消息框
        qDebug()<<"软件标识符非法";
        this->MsgDlg->critical(this,
                               "软件标识符格式非法",
                               "软件标识符只能为数字、字母和下划线组合");
    }
    // 版本号格式非法，或是空字符串
    else if(false == this->CheckSWVerIsLegal(strSWVer)
            || strSWVer.size() != 14)
    {
        // Critical消息框
        qDebug()<<"版本号非法";
        this->MsgDlg->critical(this,
                               "版本号格式非法",
                               "版本号只能为14个数字，如“01000020201226”");
    }
    else
    {
        // 标识符和版本号合法，发送信号到父窗口
        emit SendSlaveInfor(strSWLable,strSWVer);
        // 关闭子机信息窗口
        this->close();
    }

    qDebug()<<"软件标识符："<< strSWLable;
    qDebug()<<"软件版本号："<< strSWVer;
}

void SlaveInfor::on_Btn_Close_clicked()
{
    this->close();
}
